//
//  DBPackageCircleView.m
//  PictureCircle
//
//  Created by Apple on 14-9-18.
//  Copyright (c) 2014年 Apple. All rights reserved.
//

#import "DBPackageCircleView.h"
#import "UIImageView+WebCache.h"

@interface DBPackageCircleView ()<UIScrollViewDelegate>

@property (nonatomic, strong) NSMutableArray * dataArray;
@property (nonatomic, assign) NSInteger currentPageIndex;

- (void)addTimer; // 添加定时器
- (void)removeTimer; // 移除定时器

@end

@implementation DBPackageCircleView

- (id)initWithFrame:(CGRect)frame array:(NSMutableArray *)picArray
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setupScrollViewWithArray:picArray];
    }
    return self;
}

- (void)setupScrollViewWithArray:(NSMutableArray *)picArray
{
    // 图片的宽高和起始坐标
    CGFloat imageW = 320;
    CGFloat imageH = 200;
    CGFloat imageY = 0;
    // 图片数
    
    self.dataArray = [NSMutableArray  arrayWithArray:picArray];
    [self.dataArray insertObject:[picArray objectAtIndex:(picArray.count - 1)] atIndex:0];
    [self.dataArray addObject:[picArray objectAtIndex:0]];
    
    NSInteger totalCount = self.dataArray.count;
    self.totalPicCount = totalCount;
    // 轮播图
    UIScrollView * tempScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, imageW, imageH)];
    tempScrollView.showsHorizontalScrollIndicator = NO;
    self.picScrollView = tempScrollView;
        
    // 添加图片
    for (int i = 0; i < totalCount; i ++) {
        UIImageView * tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageW * i, imageY, imageW, imageH)];
        // 设置图片
        NSString * tempString1 = [[self.dataArray objectAtIndex:i] objectForKey:@"feature_img"];
        [tempImageView setImageWithURL:[NSURL URLWithString:tempString1]];
        [self.picScrollView addSubview:tempImageView];
    }
    
    // 设置scrollView的滚动范围
    self.picScrollView.contentSize = CGSizeMake(totalCount * imageW, 0);
    self.picScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    // 设置分页
    self.picScrollView.pagingEnabled = YES;
    // 监听scrollView的滚动
    self.picScrollView.delegate = self;
    [self.picScrollView setScrollEnabled:YES];
    self.picScrollView.bounces = NO;
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(260, 180, 50, 20)];
    self.pageControl.numberOfPages = totalCount - 2;
    self.pageControl.currentPage = 0;
    [self addSubview:self.picScrollView];
    [self addSubview:self.pageControl];
    [self addTimer];
}

- (void)addTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}

- (void)removeTimer
{
    [self.timer invalidate];
}

// 播放下一张图片
- (void)nextImage
{
    // 设置滚动
    CGFloat scollViewW = self.picScrollView.frame.size.width;
    CGFloat offsetX = self.picScrollView.contentOffset.x;
    int page = floor((offsetX - scollViewW / 2) / scollViewW) + 1;
    self.currentPageIndex = page + 1;
    
    if (self.currentPageIndex > 0 && self.currentPageIndex < self.dataArray.count) {
        // 滚动scrollView
        CGFloat x = self.currentPageIndex * self.picScrollView.frame.size.width;
        
        [UIView animateWithDuration:0.2 animations:^{
            self.picScrollView.contentOffset = CGPointMake(x, 0);
        }];
        
        if (self.currentPageIndex == self.dataArray.count - 1) {
            self.currentPageIndex ++;
        }

    }
    
    if (self.currentPageIndex == self.dataArray.count) {
        [_picScrollView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
    }
    
    if (self.currentPageIndex == 0) {
        [_picScrollView setContentOffset:CGPointMake((self.dataArray.count - 2) * self.bounds.size.width, 0)];
    }
   
}

#pragma mark - scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat scollViewW = self.picScrollView.frame.size.width;
    CGFloat offsetX = self.picScrollView.contentOffset.x;
    int page = floor((offsetX - scollViewW / 2) / scollViewW) + 1;
    self.currentPageIndex = page;
    self.pageControl.currentPage = page - 1;
    
//    if (self.timer.valid) {
//        if (self.currentPageIndex == (self.dataArray.count - 1)) {
//            [_picScrollView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
//        }
//    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
    self.timer = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addTimer];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.currentPageIndex == 0) {
        [_picScrollView setContentOffset:CGPointMake((self.dataArray.count - 2) * self.bounds.size.width, 0)];
    }
    
    if (self.currentPageIndex == (self.dataArray.count - 1)) {
        [_picScrollView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
